package com.ruoyi.petSys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.petSys.domain.AddFriend;
import com.ruoyi.petSys.entity.vo.AttentionVo;
import com.ruoyi.petSys.service.IAddFriendService;
import com.ruoyi.system.service.ISysUserService;
import lombok.experimental.PackagePrivate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.petSys.mapper.AttentionMapper;
import com.ruoyi.petSys.domain.Attention;
import com.ruoyi.petSys.service.IAttentionService;

/**
 * 好友列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@Service
public class AttentionServiceImpl extends ServiceImpl<AttentionMapper, Attention> implements IAttentionService
{
    @Autowired
    private AttentionMapper attentionMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IAddFriendService addFriendService;

    /**
     * 查询好友列表
     * 
     * @param id 好友列表主键
     * @return 好友列表
     */
    @Override
    public AttentionVo selectAttentionById(Long id)
    {
        return attentionMapper.selectAttentionById(id);
    }

    /**
     * 查询好友列表列表
     * 
     * @param attention 好友列表
     * @return 好友列表
     */
    @Override
    public List<AttentionVo> selectAttentionList(Attention attention)
    {
        return attentionMapper.selectAttentionList(attention).stream().filter(attentionVo -> {

            if (ObjectUtil.isNotEmpty(attentionVo.getIsDelete())&& attentionVo.getIsDelete() == 1){
                return false;
            }
            return true;
        }).collect(Collectors.toList());
    }

    /**
     * 新增好友列表
     * 
     * @param attention 好友列表
     * @return 结果
     */
    @Override
    public int insertAttention(Attention attention)
    {
        attention.setCreateTime(DateUtils.getNowDate());
        attention.setIsDelete(0);
        return attentionMapper.insertAttention(attention);
    }

    /**
     * 修改好友列表
     * 
     * @param attention 好友列表
     * @return 结果
     */
    @Override
    public int updateAttention(Attention attention)
    {

        attention.setUpdateTime(DateUtils.getNowDate());
        return attentionMapper.updateAttention(attention);
    }

    /**
     * 批量删除好友列表
     * 
     * @param ids 需要删除的好友列表主键
     * @return 结果
     */
    @Override
    public int deleteAttentionByIds(Long[] ids)
    {




            AttentionVo attentionVo = selectAttentionById(ids[0]);
            //查询好友的好友记录
            Attention attentionFriend = new Attention();
            attentionFriend.setAttentionUserId(attentionVo.getBeAttentionUserId());
            attentionFriend.setBeAttentionUserId(attentionVo.getAttentionUserId());
            List<AttentionVo> attentionVos = selectAttentionList(attentionFriend);
            if(attentionVos.size()==1){
//                attentionFriend.setId(attentionVos.get(0).getId());
//                attentionFriend.setIsDelete(1);
//                updateAttention(attentionFriend);
                deleteAttentionById(attentionVos.get(0).getId());
            }else {

                return 0;
            }
            //删除本人好友
//            Attention attentionUser = new Attention();
//            attentionUser.setId(ids[0]);
//            attentionUser.setIsDelete(1);
//            updateAttention(attentionUser);
        deleteAttentionById(ids[0]);
            return 1;


    }

    /**
     * 删除好友列表信息
     * 
     * @param id 好友列表主键
     * @return 结果
     */
    @Override
    public int deleteAttentionById(Long id)
    {
        return attentionMapper.deleteAttentionById(id);
    }


    @Override
    public List<SysUser> selectNotAttentionUserList(String nickName){
        SysUser sysUser = new SysUser();
        sysUser.setNickName(nickName);
        List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);

        Long userId = SecurityUtils.getUserId();
        Attention attention = new Attention();
        attention.setAttentionUserId(userId);
        List<Long> friendIdList = attentionMapper.selectAttentionList(attention).stream().map(e->e.getBeAttentionUserId()).collect(Collectors.toList());
        sysUsers = sysUsers.stream().filter(e->{
            if(!new Long(1).equals(e.getUserId())&&!friendIdList.contains(e.getUserId())&&!userId.equals(e.getUserId())){
                return true;
            }
            return false;
        }).collect(Collectors.toList());

        return sysUsers;
    }

    @Override
    public void updateFriendApply(AddFriend addFriend) {
        addFriend.setBeApplyUserId(SecurityUtils.getUserId());
        addFriendService.updateFriendApply(addFriend);

        if(addFriend.getIsAgree().equals(1L)){

            Attention attentionUser = new Attention();
            Attention attentionFriend = new Attention();
            attentionUser.setAttentionUserId(SecurityUtils.getUserId());
            attentionUser.setBeAttentionUserId(addFriend.getApplyUserId());
            attentionUser.setCreateTime(DateUtils.getNowDate());
            attentionFriend.setAttentionUserId(addFriend.getApplyUserId());
            attentionFriend.setBeAttentionUserId(SecurityUtils.getUserId());
            attentionFriend.setCreateTime(DateUtils.getNowDate());

            attentionMapper.insertAttention(attentionUser);
            attentionMapper.insertAttention(attentionFriend);
        }

    }


    @Override
    public void addFriendById(AddFriend addFriend) {
        Attention attention = new Attention();
        attention.setAttentionUserId(addFriend.getBeApplyUserId());
        attention.setBeAttentionUserId(addFriend.getApplyUserId());

        List<AttentionVo> attentionVos = selectAttentionList(attention);
        if(attentionVos.size()==0){


                Attention attentionUser = new Attention();
                Attention attentionFriend = new Attention();
                attentionUser.setAttentionUserId(addFriend.getBeApplyUserId());
                attentionUser.setBeAttentionUserId(addFriend.getApplyUserId());
                attentionUser.setCreateTime(DateUtils.getNowDate());
                attentionUser.setIsDelete(0);
                attentionFriend.setAttentionUserId(addFriend.getApplyUserId());
                attentionFriend.setBeAttentionUserId(addFriend.getBeApplyUserId());
                attentionFriend.setCreateTime(DateUtils.getNowDate());
                attentionFriend.setIsDelete(0);

                attentionMapper.insertAttention(attentionUser);
                attentionMapper.insertAttention(attentionFriend);

        }
    }
}
